{{- if .Site.Params.pv.busuanzi.enable }}
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
{{- else if and (.Site.Params.pv.leancloud.enable)  ( not .Site.Params.comment.valine.visitor )  }}
<!-- custom analytics part create by xiamo -->
<script defer src="https://cdn1.lncld.net/static/js/av-min-1.2.1.js"></script>
<script defer>
    AV.init({
        appId: '{{- .Site.Params.pv.leancloud.app_id }}',
        appKey: '{{- .Site.Params.pv.leancloud.app_key }}'
    });

    function showTime(Counter) {
        var query = new AV.Query(Counter);
        var visitors = $('.leancloud_visitors');
        query.greaterThanOrEqualTo("time", 0);
        query.find({
            success: function (results) {
                if (results.length == 0) {
                    return;
                }
                var data = results;
                visitors.each(function () {
                    var url = $(this).attr('id').trim();
                    for (var i = 0; i < data.length; i++) {
                        var object = data[i];
                        var content = object.get('time');
                        var _url = object.get('url')
                        if (url == _url) {
                            $(this).text(content);
                        }
                    }
                })

            },
            error: function (object, error) {
                console.log("Error: " + error.code + " " + error.message);
            }
        });
    }

    function addCount(Counter) {
        var Counter = AV.Object.extend("Counter");
        url = $(".leancloud_visitors").attr('id').trim();
        title = $(".leancloud_visitors").attr('data-flag-title').trim();
        var query = new AV.Query(Counter);
        query.equalTo("url", url);
        query.find({
            success: function (results) {
                if (results.length > 0) {
                    var counter = results[0];
                    counter.fetchWhenSave(true);
                    counter.increment("time");
                    counter.save(null, {
                        success: function (counter) {
                            var content = counter.get('time');
                            $(document.getElementById(url)).text(content);
                        },
                        error: function (counter, error) {
                            console.log('Failed to save Visitor num, with error message: ' + error.message);
                        }
                    });
                } else {
                    var newcounter = new Counter();
                    newcounter.set("title", title);
                    newcounter.set("url", url);
                    newcounter.set("time", 1);
                    newcounter.save(null, {
                        success: function (newcounter) {
                            console.log("newcounter.get('time')=" + newcounter.get('time'));
                            var content = newcounter.get('time');
                            $(document.getElementById(url)).text(content);
                        },
                        error: function (newcounter, error) {
                            console.log('Failed to create');
                        }
                    });
                }
            },
            error: function (error) {
                console.log('Error:' + error.code + " " + error.message);
            }
        });
    }
    $(function () {
        var Counter = AV.Object.extend("Counter");
        if ($('.leancloud_visitors').length == 1) {
            addCount(Counter);
        } else {
            showTime(Counter);
        }
    }); 
</script>
{{- end }}